package com.changhong.framework.sdfs.namenode.server.config;

import com.changhong.framework.sdfs.namenode.server.utils.PropertyUtil;
import com.changhong.framework.sdfs.namenode.server.utils.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * 配置管理
 * @author summer
 */
public class ConfigurationManager {
    private static final Logger logger = LoggerFactory.getLogger(ConfigurationManager.class);
    private static final String NAME_NODE_CONFIG_FILE="namenode.properties";
    private static final Properties PROPERTIES = ResourceUtils.readConfigFile(NAME_NODE_CONFIG_FILE);
    private static volatile ConfigurationManager configurationManager=null;


    private ConfigurationManager(){
    }

    public static ConfigurationManager getInstance(){
        if(configurationManager==null){
            synchronized (ConfigurationManager.class){
                configurationManager=new ConfigurationManager();
                return configurationManager;
            }
        }
        return configurationManager;
    }


    public String getString(String key){
        logger.debug("配置文件名称是："+NAME_NODE_CONFIG_FILE);
        logger.debug("传入的KEY是："+key);
        return PROPERTIES.getProperty(key);
    }

    public String getString(String key, String defaultValue) {
        logger.debug("配置文件名称是："+NAME_NODE_CONFIG_FILE);
        logger.debug("传入的KEY是{},默认值是:{}",key,defaultValue);
        return PROPERTIES.getProperty(key, defaultValue);
    }

    public Integer getInteger(String key, String defaultValue) {
        logger.debug("配置文件名称是："+NAME_NODE_CONFIG_FILE);
        logger.debug("传入的KEY是{},默认值是:{}",key,defaultValue);
        return Integer.parseInt(PROPERTIES.getProperty(key, defaultValue));
    }

    public Long getLong(String key, String defaultValue) {
        logger.debug("配置文件名称是："+NAME_NODE_CONFIG_FILE);
        logger.debug("传入的KEY是{},默认值是:{}",key,defaultValue);
        return Long.parseLong(PROPERTIES.getProperty(key, defaultValue));
    }

    public Boolean getBoolean(String key, String defaultValue) {
        logger.debug("配置文件名称是："+NAME_NODE_CONFIG_FILE);
        logger.debug("传入的KEY是{},默认值是:{}",key,defaultValue);
        return Boolean.parseBoolean(PROPERTIES.getProperty(key, defaultValue));
    }


}
